package com.pignest.hotel.ui.goods;

import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Paging;
import org.zkoss.zul.event.PagingEvent;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.TbGoods;
import com.pignest.hotel.definition.GoodsType;
import com.pignest.hotel.service.GoodsService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.MoneyUtil;

public class GoodsListbox extends Listbox {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1874017420145118951L;
	
	private GoodsService goodsService;
	private Paging page;
	private String name;
	private String type;

	public GoodsListbox()
	{
		goodsService =Services.getGoodsService();
		
	}
	public void onCreate() {

		page =  (Paging)getFellow("pagGoods");
		page.setTotalSize(goodsService.find("from TbGoods ").size());
		final int PAGE_SIZE = page.getPageSize();
		Query(0,PAGE_SIZE);
		page.addEventListener("onPaging", new EventListener() {
		      public void onEvent(Event event) {
		        	PagingEvent pe = (PagingEvent) event;
		        	int pgno = pe.getActivePage();
		        	int start = pgno*PAGE_SIZE;
		        	Query(start,PAGE_SIZE);
		      	}
		    	});

		
	}
	public void Query(int first,int pageSize){
		this.getItems().clear();
		StringBuffer hql = new StringBuffer();
		hql.append(" from TbGoods where 1=1 ");
		if(name!=null&&!name.equals(""))
		   hql.append(" and name like '%").append(name).append("%'");
		if(type!=null&&!type.equals(""))
			hql.append(" and type like '%").append(type).append("%'");
		List l =goodsService.find(hql.toString());
		int Total =l.size();
		List<TbGoods> goods = goodsService.queryPageHql(hql.toString(), first, pageSize);
		page.setTotalSize(Total);
		for(TbGoods g:goods){
			Listitem listitem = new Listitem();
			listitem.setValue(g.getId());
			listitem.setParent(this);
			listitem.appendChild(new Listcell(g.getName()));
			listitem.appendChild(new Listcell(GoodsType.valueOf(g.getType()).getCname()));
			listitem.appendChild(new Listcell(Utils.toPrice(g.getPrice())));
			Integer i =g.getCount();
			if(i!=null)
				listitem.appendChild(new Listcell(String.valueOf(i)));
			else
				listitem.appendChild(new Listcell("0"));
		}
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		System.out.println("type="+type);
		this.type = type;
	}

}
